'''
@auther tianxiaoxi  
@file test01_login by
@time 2021/12/22 20:30
'''
import json
import unittest
from api.login import LoginAPI

import app
from parameterized import parameterized


def built_data():
    test_data = []
    file = app.BASE_DIR + "/data/login.json"
    with open(file=file, encoding='utf_8') as f:
        json_data = json.load(f)
        # print(json_data)
        for case_data in json_data:
            login_data = case_data.get("login_data")
            status_code = case_data.get("status_data")
            success = case_data.get("success")
            code = case_data.get("code")
            msg = case_data.get("msg")
            test_data.append((login_data, status_code, success, code, msg))
    return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()

    def tearDown(self):
        pass

    @parameterized.expand(built_data())
    def test_logon(self, login_data, status_code, success, code, msg):
        response = self.login_api.login(login_data)

        self.assertEqual(status_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(msg, response.json().get("message"))

